package com.hcs.chenyu.moreaboutstudent;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Studentdao extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private Context context;
    private Mydbstudent dbstudent;
    private SQLiteDatabase databasestudent;

    //构造函数
    public Studentdao(Context context) {
        this.context = context;
    }

    //打开数据库
    public void open() throws SQLiteException {
        dbstudent = new Mydbstudent(context);
        try {
           databasestudent = dbstudent.getWritableDatabase();
        } catch (SQLiteException ex) {
            databasestudent = dbstudent.getReadableDatabase();
        }
    }

    //关闭数据库
    public void close() {
        if (databasestudent != null) {
            databasestudent.close();
            databasestudent = null;
        }
    }


    //添加学生个人信息
    public long addstudent(Student s) {
        // 创建ContentValues对象
        ContentValues values = new ContentValues();
        // 向该对象中插入键值对
        values.put("mstudentid", s.mstudentid);
        values.put("mstudentname", s.mstudentname);
        values.put("mstudentsex",s.mstudentsex);
        values.put("mstudentmajoy", s.mstudentmajoy);
        values.put("mstudentclass", s.mstudentclass);
        values.put("mstudenttel",s.mstudenttel);
        values.put("mstudentdormNo",s.mstudentdormNo);
        // 调用insert()方法将数据插入到数据库当中
        return databasestudent.insert("student", null, values);
    }

    //删除指定学生信息
    public int deletestudent(Student s) {
        return databasestudent.delete("student", "mstudentid=?", new String[]{String.valueOf(s.mstudentid)});
    }

    //修改指定学生信息
    public int updatestudent(Student s) {
        ContentValues value = new ContentValues();
        value.put("mstudentname", s.mstudentname);
        value.put("mstudentsex", s.mstudentsex);
        value.put("mstudentmajoy", s.mstudentmajoy);
        value.put("mstudentclass", s.mstudentclass);
        value.put("mstudenttel", s.mstudenttel);
        value.put("mstudentdormNo", s.mstudentdormNo);

        return databasestudent.update("student", value, "mstudentid=?", new String[]{String.valueOf(s.mstudentid)});
    }

    //根据学生学号查找订单
    public Student getstudents(String mstudentid) {
        //查询学生
        Cursor cursor = databasestudent.query("student", null, "mstudentid=?", new String[]{mstudentid}, null, null, null);
        Student student = new Student();
        while (cursor.moveToNext()) {
            student.mstudentid = cursor.getString(cursor.getColumnIndex("mstudentid"));
            student.mstudentname = cursor.getString(cursor.getColumnIndex("mstudentname"));
            student.mstudentsex  = cursor.getString(cursor.getColumnIndex("mstudentsex"));
            student.mstudentmajoy = cursor.getString(cursor.getColumnIndex("mstudentmajoy"));
            student.mstudentclass = cursor.getString(cursor.getColumnIndex("mstudentclass"));
            student.mstudenttel =  cursor.getString(cursor.getColumnIndex("mstudenttel"));
            student.mstudentdormNo = cursor.getString(cursor.getColumnIndex("mstudentdormNo"));

        }
        return student;
    }

    //查找所有学生个人信息
    public ArrayList<Map<String, Object>> getAllstudents() {
        ArrayList<Map<String, Object>> liststudents = new ArrayList<Map<String, Object>>();
        Cursor cursor = databasestudent.query("student", null, null, null, null, null, null);

        int resultCounts = cursor.getCount();  //记录数
        if (resultCounts == 0) {
            return null;
        } else {
            while (cursor.moveToNext()) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("mstudentid", cursor.getString(cursor.getColumnIndex("mstudentid")));
                map.put("mstudentname", cursor.getString(cursor.getColumnIndex("mstudentname")));
                map.put("mstudentsex", cursor.getString(cursor.getColumnIndex("mstudentsex")));
                map.put("mstudentmajoy", cursor.getString(cursor.getColumnIndex("mstudentmajoy")));
                map.put("mstudentclass", cursor.getString(cursor.getColumnIndex("mstudentclass")));
                map.put("mstudenttel", cursor.getString(cursor.getColumnIndex("mstudenttel")));
                map.put("mstudentdormNo", cursor.getString(cursor.getColumnIndex("mstudentdormNo")));
                liststudents.add(map);
            }
            return liststudents;
        }
    }
}